package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 

    private long id;
    private String title;
    private String author;
    private String releaseDate;

    private long serialNo;
    private String acqDate;
    private double acqPrice;

    public DvdUI()
    {
        
    }

    public void start()
    {
        // Starts an instance of DvdUI
        dvdCtr = new DVDCtr();
        dvdMenu();
    }

    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if (choice == 1)
            {
                findDVD();
            }
            else{
                if(choice == 2){
                    createDVD();
                }
                else{
                    if(choice == 3){
                        updateDVD();
                    }
                    else{
                        if(choice == 4){
                            deleteDVD();
                        }
                        else{
                            if(choice == 5){
                                createCopy();
                            }
                            else{
                                if(choice == 6){
                                    updateCopy();
                                }
                                else{
                                    if(choice == 7){
                                        deleteCopy();
                                    }
                                    else{
                                        if(choice == 8){
                                            listAllDvds();
                                        }
                                        else{
                                            exit = true;
                                        }//end else
                                    }//end else
                                }//end else
                            }//end else
                        }//end else

                    }//end while
                }
            }
        }
    }

    private int writeDVDMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD description");
        System.out.println(" (3) Update DVD");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Create Copy");
        System.out.println(" (6) Update Copy");
        System.out.println(" (7) Delete Copy");
        System.out.println(" (8) Show current DVD list");
        System.out.println(" (9) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }
    
    public long inputDVDNo()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write DVD Number:  ");
            try{
                id = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }
    
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }

    private String inputAuthor()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD's Author: " );
        String author = keyboard.nextLine();
        return author;
    }
    
    public long inputCopyNo()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Copy number:  ");
            try{
                serialNo = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return serialNo;
    }
    
    private String inputReleaseDate()
    {
        //// creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD's releaseDate: " );
        String releaseDate = keyboard.nextLine();
        return releaseDate;
    }

    private String inputAcqDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write acquirement date: " );
        String acqDate = keyboard.nextLine();
        return acqDate;
    }

    public double inputAcqPrice()
    {   
        boolean ok = false;
        double acqPrice = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write acquirement price:  ");
            try{
                acqPrice = keyboard.nextDouble();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return acqPrice;
    }
    
    public void inputDefault()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Press ENTER to continue: ");
        String enter = keyboard.nextLine();
    }
    
    private void findDVD()
    {     
        // Gets the information about a dvd
        long id = inputDVDNo();        
        dvdCtr.getDVD(id);
        System.out.println("");
        inputDefault();
    }

    private void createDVD()
    {
        // Creates a new dvd
        id = inputDVDNo();
        title = inputTitle();
        author = inputAuthor();
        releaseDate = inputReleaseDate();

        dvdCtr.createDVD(id, title, author, releaseDate);
        System.out.println("");
        inputDefault();
    }

    private void createCopy()
    {
        // Creates a copy of a dvd
        id = inputDVDNo();
        serialNo = inputCopyNo();
        acqDate = inputAcqDate();
        acqPrice = inputAcqPrice();

        dvdCtr.createSample(id, serialNo, acqDate, acqPrice);
        System.out.println("");
        inputDefault();
    }

    private void deleteDVD()
    {
        // Deletes a dvd
        id = inputDVDNo();      
        dvdCtr.deleteDVD(id);
        System.out.println("");
        inputDefault();
    }

    private void deleteCopy()
    {
        // Deletes a copy of a dvd
        id = inputDVDNo(); 
        serialNo = inputCopyNo();
        dvdCtr.deleteSample(id, serialNo);
        System.out.println("");
        inputDefault();
    }

    private void updateDVD()
    {
        // Edits the information about a dvd
        id = inputDVDNo();
        title = inputTitle();
        author = inputAuthor();
        releaseDate = inputReleaseDate();

        dvdCtr.updateDvd(id, title, author, releaseDate);
        System.out.println("");
        inputDefault();
    }

    private void updateCopy()
    {
        // Edites the iformation about a copy of a dvd
        id = inputDVDNo();
        serialNo = inputCopyNo();
        acqDate = inputAcqDate();
        acqPrice = inputAcqPrice();

        dvdCtr.updateSample(id, serialNo, acqDate, acqPrice);
        System.out.println("");
        inputDefault();
    }

    private void listAllDvds()
    {
        // Prints the information about all of the dvds and copys
        dvdCtr.listAllDvds();
        System.out.println("");
        inputDefault();
    }
}